package com.engine.zhlx.zhyq.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.zhlx.zhyq.utils.MeetingUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-05-10
 * @Description: 调用自定义会议接口
 * @ModifyLog:
 **/

public class WeaMeetingWeb extends BaseBean {
    /**
     * @Description: 获取占用会议室列表
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023/7/17
     */
    @POST
    @Path("/queryUsedMeetingRoomList")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String queryUsedMeetingRoomList(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        MeetingUtil meetingUtil = new MeetingUtil();
        try {
            writeLog( ">>>>>queryUsedMeetingRoomList>>>paramsJson=" + paramsJson );
            String companyId = Util.null2String( paramsJson.getString( "companyId" ) );
            writeLog( ">>>>>queryUsedMeetingRoomList>>>companyId=" + companyId );
            String sql = " select id,name from MeetingRoom where status=1 ";
            String sqlwhere = "";
            String addrees = meetingUtil.getAddress();
            if (!"".equals( addrees )) {
                sqlwhere += " and id in (" + addrees + ")";
            }

            if (!"".equals( companyId )) {
                sqlwhere += " and subcompanyid='" + companyId + "'";
            }
            rs.executeQuery( sql + sqlwhere );
            writeLog( ">>>>>queryUsedMeetingRoomList>>>sql=" + sql + sqlwhere );
            JSONArray dataList = new JSONArray();
            while (rs.next()) {
                JSONObject jsonData = new JSONObject();
                String id = Util.null2String( rs.getString( "id" ) );
                String name = Util.null2String( rs.getString( "name" ) );
                jsonData.put( "meetingRoomId", id );
                jsonData.put( "meetingRoomCode", id );
                jsonData.put( "meetingRoomName", name );
                jsonData.put( "roomRemark", "" );
                dataList.add( jsonData );
            }
            writeLog( ">>>>>queryUsedMeetingRoomList>>>dataList=" + dataList );
            if (dataList.size() > 0) {
                apidatas.put( "code", "SUCCESS" );
                apidatas.put( "data", dataList );
                apidatas.put( "message", "查询信息成功" );
            } else {
                apidatas.put( "code", "SUCCESS" );
                apidatas.put( "data", dataList );
                apidatas.put( "message", "未查询到信息" );
            }
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "code", "SYSTEM_INNER_ERROR" );
            apidatas.put( "data", "" );
            apidatas.put( "message", "请求JSON数据格式异常" );
        }
        writeLog( ">>>>>queryMeetingRoomList>>>apidatas=" + apidatas );
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 获取可用会议室列表
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023/7/17
     */
    @POST
    @Path("/queryUnusedMeetingRoomList")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String queryUnusedMeetingRoomList(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        MeetingUtil meetingUtil = new MeetingUtil();
        try {
            writeLog( ">>>>>queryUnusedMeetingRoomList>>>paramsJson=" + paramsJson );
            String companyId = Util.null2String( paramsJson.getString( "companyId" ) );
            writeLog( ">>>>>queryUnusedMeetingRoomList>>>companyId=" + companyId );
            String sql = " select id,name from MeetingRoom where status=1 ";
            String sqlwhere = "";
            String addrees = meetingUtil.getAddress();
            if (!"".equals( addrees )) {
                sqlwhere += " and id not in (" + addrees + ")";
            }

            if (!"".equals( companyId )) {
                sqlwhere += " and subcompanyid='" + companyId + "'";
            }
            rs.executeQuery( sql + sqlwhere );
            writeLog( ">>>>>queryUnusedMeetingRoomList>>>sql=" + sql + sqlwhere );
            JSONArray dataList = new JSONArray();
            while (rs.next()) {
                JSONObject jsonData = new JSONObject();
                String id = Util.null2String( rs.getString( "id" ) );
                String name = Util.null2String( rs.getString( "name" ) );
                jsonData.put( "meetingRoomId", id );
                jsonData.put( "meetingRoomCode", id );
                jsonData.put( "meetingRoomName", name );
                jsonData.put( "roomRemark", "" );
                dataList.add( jsonData );
            }
            writeLog( ">>>>>queryUnusedMeetingRoomList>>>dataList=" + dataList );
            if (dataList.size() > 0) {
                apidatas.put( "code", "SUCCESS" );
                apidatas.put( "data", dataList );
                apidatas.put( "message", "查询信息成功" );
            } else {
                apidatas.put( "code", "SUCCESS" );
                apidatas.put( "data", dataList );
                apidatas.put( "message", "未查询到信息" );
            }
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "code", "SYSTEM_INNER_ERROR" );
            apidatas.put( "data", "" );
            apidatas.put( "message", "请求JSON数据格式异常" );
        }
        writeLog( ">>>>>queryUnusedMeetingRoomList>>>apidatas=" + apidatas );
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 获取会议列表
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023/7/17
     */
    @POST
    @Path("/queryMeetingList")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String queryMeetingList(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        MeetingUtil meetingUtil = new MeetingUtil();
        try {
            writeLog( ">>>>>queryMeetingList>>>paramsJson=" + paramsJson );
            String beginDate = Util.null2String( paramsJson.getString( "beginDate" ) );
            String endDate = Util.null2String( paramsJson.getString( "endDate" ) );
            String meetingRoomId = Util.null2String( paramsJson.getString( "meetingRoomId" ) );
            writeLog( ">>>>>queryMeetingList>>>beginDate=" + beginDate + ",endDate=" + endDate );
            writeLog( ">>>>>queryMeetingList>>>meetingRoomId=" + meetingRoomId );
            String sql = " select id,name,meetingtype,address,caller,begindate,begintime,enddate,endtime,\n" +
                    "repeattype,meetingstatus,desc_n,hrmmembers,qrticket from meeting ";
            String sqlwhere = " where 1=1 and caller !=1 ";
            if (!"".equals( beginDate )) {
                sqlwhere += " and begindate>='" + beginDate + "'";
            }
            if (!"".equals( endDate )) {
                sqlwhere += " and enddate<='" + endDate + "'";
            }
            if (!"".equals( meetingRoomId )) {
                sqlwhere += " and address in (" + meetingRoomId + ")";
            }
            rs.executeQuery( sql + sqlwhere );
            writeLog( ">>>>>queryMeetingList>>>sql=" + sql + sqlwhere );
            JSONArray dataList = new JSONArray();
            while (rs.next()) {
                JSONObject jsonData = new JSONObject();
                String id = Util.null2String( rs.getString( "id" ) );
                String name = Util.null2String( rs.getString( "name" ) );
                String meetingtype = Util.null2String( rs.getString( "meetingtype" ) );
                String address = Util.null2String( rs.getString( "address" ) );
                String caller = Util.null2String( rs.getString( "caller" ) );
                String begindate = Util.null2String( rs.getString( "begindate" ) );
                String begintime = Util.null2String( rs.getString( "begintime" ) );
                String enddate = Util.null2String( rs.getString( "enddate" ) );
                String endtime = Util.null2String( rs.getString( "endtime" ) );
                String repeattype = Util.null2String( rs.getString( "repeattype" ) );
                String meetingstatus = Util.null2String( rs.getString( "meetingstatus" ) );
                String descN = Util.null2String( rs.getString( "desc_n" ) );
                String hrmmembers = Util.null2String( rs.getString( "hrmmembers" ) );
                String qrticket = Util.null2String( rs.getString( "qrticket" ) );
                jsonData.put( "meetingId", id );
                jsonData.put( "meetingTitle", name );
                jsonData.put( "meetingType", meetingtype );
                jsonData.put( "meetingRoomId", address );
                JSONObject userInfo = meetingUtil.getUserInfo( caller );
                jsonData.put( "reserveEmployeeNum", userInfo.getString( "workcode" ) );
                jsonData.put( "reserveEmployeeName", userInfo.getString( "lastname" ) );
                jsonData.put( "meetingStartTime", begindate + " " + begintime );
                jsonData.put( "meetingEndTime", enddate + " " + endtime );
                jsonData.put( "cycleMeetingTag", "0".equals( repeattype ) ? "NO" : "YES" );
                String meetingStatus = meetingUtil.convertMeetStat( meetingstatus,id);
                jsonData.put( "meetingStatus", meetingStatus );
                jsonData.put( "meetingRemark", descN );
                JSONArray participants = meetingUtil.getUserList( hrmmembers );
                jsonData.put( "participants", participants );
                jsonData.put( "qrcode", qrticket );
                dataList.add( jsonData );
            }
            writeLog( ">>>>>queryMeetingList>>>dataList=" + dataList );
            if (dataList.size() > 0) {
                apidatas.put( "code", "SUCCESS" );
                apidatas.put( "data", dataList );
                apidatas.put( "message", "查询信息成功" );
            } else {
                apidatas.put( "code", "SUCCESS" );
                apidatas.put( "data", dataList );
                apidatas.put( "message", "未查询到信息" );
            }
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "code", "SYSTEM_INNER_ERROR" );
            apidatas.put( "data", "" );
            apidatas.put( "message", "请求JSON数据格式异常" );
        }
        writeLog( ">>>>>queryMeetingList>>>apidatas=" + apidatas );
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 获取签到信息列表
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2023/7/17
     */
    @POST
    @Path("/querySignInfList")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)

    public String querySignInfList(JSONObject paramsJson) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        MeetingUtil meetingUtil = new MeetingUtil();
        try {
            writeLog( ">>>>>querySignInfList>>>paramsJson=" + paramsJson );
            String meetingId = Util.null2String( paramsJson.getString( "meetingId" ) );
            writeLog( ">>>>>querySignInfList>>>meetingId=" + meetingId );
            String sql = " select id,meetingid,userid,signtime from meeting_sign ";
            String sqlwhere = " where 1=1";
            if (!"".equals( meetingId )) {
                sqlwhere += " and meetingid='" + meetingId + "'";
            }
            rs.executeQuery( sql + sqlwhere );
            writeLog( ">>>>>querySignInfList>>>sql=" + sql + sqlwhere );
            JSONArray dataList = new JSONArray();
            while (rs.next()) {
                JSONObject jsonData = new JSONObject();
                String meetingid = Util.null2String( rs.getString( "meetingid" ) );
                String userid = Util.null2String( rs.getString( "userid" ) );
                JSONObject userInfo = meetingUtil.getUserInfo( userid );
                String signtime = Util.null2String( rs.getString( "signtime" ) );
                jsonData.put( "meetingId", meetingid );
                jsonData.put( "employeeNum", userInfo.getString( "workcode" ) );
                jsonData.put( "employeeName", userInfo.getString( "lastname" ) );
                jsonData.put( "signInTime", signtime );
                dataList.add( jsonData );
            }
            writeLog( ">>>>>querySignInfList>>>dataList=" + dataList );
            if (dataList.size() > 0) {
                apidatas.put( "code", "SUCCESS" );
                apidatas.put( "data", dataList );
                apidatas.put( "message", "查询信息成功" );
            } else {
                apidatas.put( "code", "SUCCESS" );
                apidatas.put( "data", dataList );
                apidatas.put( "message", "未查询到信息" );
            }
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "code", "SYSTEM_INNER_ERROR" );
            apidatas.put( "data", "" );
            apidatas.put( "message", "请求JSON数据格式异常" );
        }
        writeLog( ">>>>>querySignInfList>>>apidatas=" + apidatas );
        return JSONObject.toJSONString( apidatas );
    }


}
